<template>
  <div class="pages xzmember">
    <div class="from_main">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="等级名称：" prop="name">
          <el-input
            v-model="ruleForm.name"
            size="small"
            placeholder="最多15个字符"
            clearable
            maxlength="15"
          ></el-input>
        </el-form-item>
        <el-form-item label="等级说明：" prop="levelDescribe">
          <textarea
            maxlength="20"
            v-model="ruleForm.levelDescribe"
            name
            id
            cols="10"
            rows="5"
            placeholder="输入等级说明，最多20字"
            style="width:220px;resize:none;border-radius:5px;"
          ></textarea>
        </el-form-item>
        <el-form-item label="会员优惠：" prop="memberDiscount" class="memberStyle">
          <el-radio-group v-model="ruleForm.discountType" @change="changePrice">
             <div style="margin-top:10px;">
             <el-radio label="2" value="2">不优惠</el-radio>
            </div>
             <div style="margin-top:20px;">
              <el-radio label="1" value="1">会员价</el-radio>
              <el-form-item label label-width="15px" style="display:inline-block;">
                 <el-select filterable size="small" style="width:180px" :disabled="disablePriceone"  v-model="ruleForm.memberpricelevelid" placeholder="全部" clearable>
                  <el-option v-for="item in commodityBrandList" :key="item.id" :label="item.name" :value="item.id"> </el-option>
                 </el-select>
                  <el-form-item style="display:inline-block;" label label-width="15px">
                 <span> 服务类和次卡使用唯一的会员价</span>
               </el-form-item> 
              </el-form-item>
          
            </div>
            <div style="margin-top:20px;">
              <el-radio label="0" value="0">会员折扣</el-radio>
            <el-input
              style="width:200px;"
              v-model="ruleForm.memberDiscount"
              size="small"
              placeholder="最多保留小数点后2位"
              clearable
              maxlength="8"
              :disabled="disablePrice"
            ></el-input>
            <el-form-item style="display:inline-block;" label label-width="15px">
              <span>折 (打85折时填写8.5，服务类和次卡不参与会员折扣)</span>
            </el-form-item>
            <br />
            </div>
           </el-radio-group>
        </el-form-item>
        <el-form-item label="升级奖励：">
          <el-checkbox-group v-model="ruleForm.putChannel" @change="checkPoints" >
            <el-checkbox label="0" value='1'>赠送积分</el-checkbox>
              <el-input
              style="width:200px;"
              v-model="ruleForm.rewardIntegral"
              size="small"
              placeholder="请填写正整数"
              clearable
              maxlength="8"
              onkeyup="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}"
              onafterpaste="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}"
              :disabled="disablePrices"
            ></el-input>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item :formatter="formatState3" label="状 态" prop="status">
          <el-radio-group v-model="ruleForm.status">
            <el-radio label="0" value="0">启用</el-radio>
            <el-radio label="1" value="1">停用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item style="margin-left:300px;margin-top:90px">
          <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
          <el-button @click="resetForm('ruleForm')">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
    <script>
import { saveMemberLevel ,goodsMemberPriceList} from "@/api";
import { mapState } from "vuex";
import u from "@/util";
export default {
  computed: mapState(["sub", "power", "merchantId"]),
  data() {
    return {
      showjf: true,
      ruleForm: {
        name: "",
        levelDescribe: "",
        discountType:"1",
        memberDiscount:"",
        memberpricelevelid:"",
        rewardIntegral:"",
        status:"0",
        putChannel:false,
      },//搜索条件
      rules: {
        name: [
          { required: true, message: "请输入会员等级名称", trigger: "blur" },
          { min: 1, max: 30, message: "长度在 1到 30个字符", trigger: "blur" },
          { validator: u.valida, trigger: "blur" }
        ],
        memberDiscount: [
          { required: false, message: "请输入会员优惠", trigger: "blur" },
          {
              validator: u.validatenums,
              trigger: "blur",
              required: false
            }
        ]
      },
      disablePrice:true,
      disablePrices:true,
      disablePriceone:false,
      commodityBrandList:[],
    };
  },
  mounted(){
   this._goodsMemberPriceList()
  },
  methods: {
    formatState1: function(row, column) {
      return row.state == 0 ? "手动升级" : row.state == 1 ? "自动升级" : "";
    },
    formatState3: function(row, column) {
      return row.state == 0 ? "启用" : row.state == 1 ? "停用" : "";
    },
    // 选择会员优惠
    changePrice(val){
      if(val=='0'){
        this.disablePrice=false;
        this.disablePriceone=true;
        this.ruleForm.memberpricelevelid=""
      }else if(val=='1'){
        this.disablePriceone=false;
        this.disablePrice=true;
        this.ruleForm.memberDiscount=""
      }else{
         this.disablePriceone=true;
         this.disablePrice=true;
         this.ruleForm.memberDiscount=""
         this.ruleForm.memberpricelevelid=""
      }
    },
    //  会员等级
    _goodsMemberPriceList(){
     goodsMemberPriceList().then(res=>{
       this.commodityBrandList=res.data
       this.ruleForm.memberpricelevelid=this.commodityBrandList[0].id
     })
    },
    // 升级奖励
    checkPoints(val){
     if(val==true){
       this.disablePrices=false;
     }else{
       this.disablePrices=true;
     }
    },
     // 新增
    list() {
      let Data = {
        companyId: this.merchantId,
        name: this.ruleForm.name,
        levelDescribe: this.ruleForm.levelDescribe,
        discountType:this.ruleForm.discountType,
        memberDiscount: this.ruleForm.memberDiscount,
        memberPriceLevelId: this.ruleForm.memberpricelevelid,
        rewardIntegral: this.ruleForm.rewardIntegral,
        status: this.ruleForm.status
      };
      if(this.ruleForm.discountType=='1'&&this.ruleForm.memberpricelevelid==''){
           this.$message({
            type: "error",
            message: "请选择会员价"
          });
      }else if(this.ruleForm.discountType=='0'&&this.ruleForm.memberDiscount==''){
            this.$message({
            type: "error",
            message: "请输入会员折扣"
          });
      }else{
        saveMemberLevel(Data).then(res => {
         if (res.code == 200) {
          this.$message({
            type: "success",
            message: res.msg
          });
          u.routerTo("/operate-management/member-management/member-level");
        } 
      });
      }
     
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.list();
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      history.back();
    }
  }
};
</script>
    <style scoped>
.from_main .el-input {
  width: 350px;
}
.inp {
  width: 400px;
  height: 40px;
  position: absolute;
  top: 3px;
  left: 340px;
}

.inp input {
  border: 1px solid #ccc;
  border-radius: 8px;
  line-height: 38px;
  text-indent: 6px;
  float: left;
}
.inp h1 {
  color: #606266;
  float: left;
  font-size: 14px;
}
.font h2 {
  width: 100px;
  position: absolute;
  top: 10px;
  left: 755px;
  color: #606266;
  font-size: 14px;
}
.font h6 {
  width: 130px;
  position: absolute;
  top: 0px;
  left: 270px;
  color: #606266;
  font-size: 12px;
  letter-spacing: 2px;
}
.font {
  margin-top: 20px;
  width: 350px;
}
</style>
<style lang="">
.memberStyle .el-form-item__error {
  /* top: 40% !important; */
  left: 9% !important;
}
</style>
